﻿$(function () {
    supplierShow()
})
function supplierShow() {
    layui.use(function () {
        var table = layui.table;
        console.log(table)
        // 先进行表格的初始渲染
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'supplierID', title: '供应商ID' },
                { field: 'supplierNumber', title: '供应商编号' },
                { field: 'supplierName', title: '供应商名称' },
                { field: 'supplierType', title: '供应商类别' },
                { field: 'supplierPrincipal', title: '负责人' },
                { field: 'supplierPhone', title: '联系电话' },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.supplierID + ')"><i class="layui-icon layui-icon-edit"></i>添加供应商</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.supplierID + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }
                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        // 创建表格实例
        $.ajax({
            url: "/Supplier/LedgerInfoSupplierShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 5,
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
    });
}

layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    // 打开添加弹窗的按钮点击事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: "新增供应商",
                content: `
                    <form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">供应商编号</label>
                    <div class="layui-input-inline">
                    <input type="text" id="supplierNumber" name="supplierNumber" autocomplete="off" placeholder="请输入供应商编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">供应商名称</label>
                    <div class="layui-input-inline">
                    <input type="text" name="supplierName" autocomplete="off" placeholder="请输入供应商名称" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">供应商类别</label>
                    <div class="layui-input-inline">
                    <textarea name="supplierType" placeholder="请输入供应商类别" class="layui-textarea"></textarea>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">负责人</label>
                    <div class="layui-input-inline">
                    <input type="text" name="supplierPrincipal" autocomplete="off" placeholder="请输入负责人" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-inline">
                    <input type="text" name="supplierPhone" autocomplete="off" placeholder="请输入联系电话" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                     <button class="layui-btn" lay-submit lay-filter="demo-login">添加</button>
                    </div>
                    <form>`,
                success: function () {
                   

                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field;
                        console.log(field)
                        $.ajax({
                            url: '/Supplier/LedgerInfoSupplierAdd',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                console.log(res)
                                if (s.code == 1) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });
            // 获取当前日期时间
            var now = new Date();
            var hours = ("0" + now.getHours()).slice(-2);
            var minutes = ("0" + now.getMinutes()).slice(-2);
            var seconds = ("0" + now.getSeconds()).slice(-2);
            var serialNumber = "GYS" + hours + minutes + seconds;
            console.log(serialNumber)
            $("#supplierNumber").val(serialNumber)

        }
    })

    window.deleteData = function (id) {
        console.log(id);
        layer.confirm('确定要删除该条数据吗？', function (index) {
            $.ajax({
                type: "Post",
                url: "/Supplier/LedgerInfoSupplierDelete",  // 替换为实际的后台删除数据接口地址
                data: { data: JSON.stringify({ supplierID: id }), token: localStorage.getItem("token") },
                success: function (res) {
                    var res = JSON.parse(res)
                    if (res.code >= 1) {
                        layer.alert("删除成功");
                        supplierShow();  // 重新加载数据，刷新表格显示
                    } else {
                        layer.alert("删除失败");

                    }
                },
                error: function () {
                    layer.msg('删除失败，请稍后重试', {
                        time: 2000,
                        icon: 2
                    });
                }
            });
            layer.close(index);
        });
    };

});



